#include "../include/jcHSV.h"
#include <stdio.h>

jcHSV::jcHSV()
{
   //Htotal=18;
   //Stotal=3;
   //Vtotal=3;
   //indiceTotal = Htotal * Stotal * Vtotal;
};

jcHSV::~jcHSV()
{

};

void jcHSV::jcSetHistHSV(IplImage* img)
{
    int indiceTotal = Htotal*Stotal*Vtotal;
    
    for(int j=0;j<indiceTotal;j++) 
      HistHSV[j]=0;
      
    cvCvtColor(img,img,CV_RGB2HSV);
    
    int x,y;
    float h,s,v;
    int indice;
    int Lin = img->width;
    int Col = img->height;
    for (y=0; y < Lin; y++)
    {
       for (x=0; x < Col; x++)
       {
         v = ((uchar*)(img->imageData + img->widthStep*x))[y*3+2];
         s = ((uchar*)(img->imageData + img->widthStep*x))[y*3+1];
         h = ((uchar*)(img->imageData + img->widthStep*x))[y*3+0];
         indice = ((cvRound((h/180)*(Htotal-1))) + cvRound((h/255)*(Stotal-1)) + cvRound((v/255)*(Vtotal-1)));
         //printf("h = %f\n s = %f\n v = %f\n indice = %f\n",h,s,v,indice);
         //system("pause");
         HistHSV[indice] = HistHSV[indice] + 1.0;
         if(!(h>90 && h<220))
         {
             ((uchar*)(img->imageData + img->widthStep*x))[y*3+0] = 0;
             ((uchar*)(img->imageData + img->widthStep*x))[y*3+1] = 0;
             ((uchar*)(img->imageData + img->widthStep*x))[y*3+2] = 0;
         }
       }
    }
    
    int TOTAL = Lin * Col;
    for( int j = 0; j<indiceTotal; j++)
    {
        HistHSV[j] = HistHSV[j]/TOTAL;
        //printf("HistHSV[%i] = %f\n", j,HistHSV[j]);
    }
}

float jcHSV::jcGetHistHSV(int indice)
{
    return(HistHSV[indice]);
}

void jcHSV::jcHSVSegmentation(IplImage* src, IplImage* dst)
{
    cvCvtColor(src,dst,CV_RGB2HSV);
    
    int x,y;
    float h;
    //float s,v;
    int Lin = dst->width;
    int Col = dst->height;
    for (y=0; y < Lin; y++)
    {
       for (x=0; x < Col; x++)
       {
          //v = ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+2];
          //s = ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+1];
          h = ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+0];
          if((h>60 && h<66))
          //if(!(h>90 && h<220))
          {
             ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+0] = 0;
             ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+1] = 0;
             ((uchar*)(dst->imageData + dst->widthStep*x))[y*3+2] = 0;
          }
       }
    }
}
